package com.lanhe.admin.controller.notice;


import com.lanhe.common.annotation.Log;
import com.lanhe.common.core.controller.BaseController;
import com.lanhe.common.core.domain.AjaxResult;
import com.lanhe.common.core.page.TableDataInfo;
import com.lanhe.common.enums.BusinessType;
import com.lanhe.notice.domain.LhNotice;
import com.lanhe.notice.service.ILhNoticeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 公告表(LhNotice)表控制层
 *
 * @author zhanggl
 * @since 2023-12-30 14:00:35
 */
@RestController
@RequestMapping("lhNotice")
public class LhNoticeController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private ILhNoticeService lhNoticeService;

    /**
     * 分页查询所有数据
     */
    @PreAuthorize("@ss.hasPermi('notice:lhNotice:list')")
    @GetMapping("/list")
    public TableDataInfo list(LhNotice notice) {
        startPage();
        List<LhNotice> list = lhNoticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    /**
     * 根据编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('notice:lhNotice:query')")
    @GetMapping(value = {"/", "/{noticeId}"})
    public AjaxResult getInfo(@PathVariable(value = "noticeId") Long noticeId) {
        return success(lhNoticeService.selectNoticeById(noticeId));
    }

    /**
     * 新增公告
     */
    @PreAuthorize("@ss.hasPermi('notice:lhNotice:add')")
    @Transactional
    @Log(title = "公告管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody LhNotice notice) {

        notice.setCreateBy(getUsername());
        notice.setCreateTime(new Date());
        return toAjax(lhNoticeService.insertNotice(notice));
    }

    /**
     * 修改新增公告
     */
    @PreAuthorize("@ss.hasPermi('notice:lhNotice:edit')")
    @Log(title = "公告管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody LhNotice notice) {
        if (ObjectUtils.isEmpty(notice.getNoticeId())
                || null == notice.getNoticeId()) {
            return error("主键ID不能为空");
        }
        notice.setUpdateBy(getUsername());
        notice.setUpdateTime(new Date());
        return toAjax(lhNoticeService.updateNotice(notice));
    }

    /**
     * 删除新增公告
     */
    @PreAuthorize("@ss.hasPermi('notice:lhNotice:remove')")
    @Log(title = "公告管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeId}")
    public AjaxResult remove(@PathVariable Long noticeId)
    {
        if (ObjectUtils.isEmpty(noticeId)){
            return error("主键ID不能为空");
        }
        return toAjax(lhNoticeService.deleteNoticeByIds(noticeId));
    }
}

